package com.gujiangbo.application.utils;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gujiangbo.application.base.BaseOutVo;

public class SafeIdentificationCheck
{
  private static Log debugLog = LogFactory.getLog(SafeIdentificationCheck.class);
  private static SafeIdentificationCheck safeIdentificationCheck;
  
  public static SafeIdentificationCheck getInstantce()
  {
    if (safeIdentificationCheck == null) {
      safeIdentificationCheck = new SafeIdentificationCheck();
    }
    return safeIdentificationCheck;
  }
  
  public Map<String, String> getHeaderMap(HttpServletRequest request)
  {
    Map<String, String> map = new HashMap();
    String client_id = request.getHeader("client_id");
    String signature = request.getHeader("signature");
    String appType = request.getHeader("appType");
    String sourceId = request.getHeader("sourceId");
    String userIp = request.getHeader("userIp");
    String traceid = request.getHeader("traceid");
    String client_info = request.getHeader("root-client-info");
    map.put("client_id", client_id);
    map.put("signature", signature);
    map.put("appType", appType);
    map.put("sourceId", sourceId);
    map.put("userIp", userIp);
    map.put("traceid", traceid);
    map.put("root-client-info", client_info);
    return map;
  }
  
  public BaseOutVo byHeaderIdentificationCheck(HttpServletRequest request)
  {
	  BaseOutVo resp = new BaseOutVo();
    try
    {
      if (StringUtils.isBlank((CharSequence)getHeaderMap(request).get("client_id")))
      {
        resp.setReturnCode(ResourceTool.getMessage("log.request.code.safe.check.no.pass"));
        resp.setReturnMsg(ResourceTool.getMessage("log.request.desc.safe.check.no.pass") + 
          ":client_id is blank");
      }
      else
      {
        resp.setReturnCode("000000");
      }
    }
    catch (Exception e)
    {
      debugLog.error("safe Identification check exception", e);
      resp.setReturnCode(ResourceTool.getMessage("log.request.code.safe.check.no.pass"));
    }
    return resp;
  }
  
  public Map<String, String> checkReqDisableMigu(Map<String, String> headerMap, Map<String, String> extmap)
  {
    String appType = (String)headerMap.get("appType");
    String sourceId = (String)headerMap.get("sourceId");
    if ((StringUtils.isBlank(appType)) || (StringUtils.isBlank(sourceId)))
    {
      debugLog.error("MIGU:appType or sourceId is empty");
      return null;
    }
    if (extmap == null) {
      extmap = new HashMap();
    }
    extmap.put("appType", appType);
    extmap.put("sourceId", sourceId);
    return extmap;
  }
}

